package com.jyhg.core.common.utils
{
	import com.greensock.TweenMax;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundLoaderContext;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;
	
	/**
	 * SoundManager
	 * 声音控制类
	 * @author jack longliangyou.jack@snda.com
	 * $Id: SoundManager.as 20 2011-07-13 06:05:26Z longliangyou $
	 * @version 1.0
	 */
	public class SoundManager
	{
		/**
		 * 默认声音大小
		 */		
		public var defaultVolume:Number=1.0;
		
		private var activeSound:Dictionary;
		public var old_bg_music:String;
		public function SoundManager():void
		{
			activeSound = new Dictionary();
		}
		
		/**域资源的统一的地址前缀**/
		public var resUrl:String="";
		
		/**
		 * 获取单例 
		 * @return 
		 * 
		 */
		private static var instance:SoundManager;
		public static function getInstance():SoundManager
		{
			if(instance==null)
				instance=new SoundManager();
			return instance;
		}
		
		
		/**
		 * 设置全局声音的大小
		 *  
		 * @param volume	声音
		 * @param pan	声音位置，范围由-1到1
		 * @param len	变化需要的时间
		 */	
		public function setGlobalVolume(volume:Number,len:Number=0):void
		{
			if (len==0)
				SoundMixer.soundTransform = new SoundTransform(volume);
//			else
//				Tweener.addTween(SoundMixer.soundTransform, {
//					volume:volume
//				});
		}
		/**
		 * 设置声音的大小
		 *  
		 * @param name	名称
		 * @param volume	声音
		 * @param len	变化需要的时间
		 */	
		public function setVolume(name:String, volume:Number,len:Number = 0):void
		{
			var sc:SoundChannel = getActiveChannel(name);
			if (sc)
			{
				if (len==0)
					sc.soundTransform = new SoundTransform(volume);
//				else
//					Tweener.addTween(sc.soundTransform, {
//						volume:volume,
//						time: len
//					});
			}
		}
		
		/**
		 * 设置声音位置
		 * 
		 * @param name	名称
		 * @param pan	声音位置，范围由-1到1
		 * @param len	过渡时间
		 */
		public function setPan(name:String, pan:Number,len:Number):void
		{
			var sc:SoundChannel = getActiveChannel(name);
			if (sc)
			{
				if (len==0)
					sc.soundTransform = new SoundTransform(sc.soundTransform.volume,pan);
//				else
//					Tweener.addTween(sc.soundTransform,{
//						pan:pan,
//						time:len
//						});
			}
		}
		
		/**
		 * 声音是否正在播放
		 *  
		 * @param name	名称
		 * 
		 */	
		public function isPlaying(name:String):Boolean
		{
			return activeSound[resUrl+name]!=null;
		}
		
		/**
		 * 停止播放
		 *  
		 * @param name	名称
		 * @param len	渐隐需要的时间
		 */	
		public function stop(name:String,len:Number=0):void
		{
//			name = name;
			
			var sc:SoundChannel = activeSound[name];
			
			if (sc)
			{
				if (len==0)
				{
					sc.stop();
					delete activeSound[name];
				}
				else
					TweenMax.to(sc,len,{volume:0,ease:"easeOutQuad",onComplete:function():void{
						sc.stop();
						delete activeSound[name];
					}});

			}
		}
		
		public function stopAll(len:Number = 0):void
		{
			for (var name:String in activeSound)
			{
				stop(name,len)
			}
		}
		
		public function getActiveChannel(name:String):SoundChannel
		{
			return activeSound[name];
		}
		
		
//		public function soundPlay(mapConfig:MapConfigVO):void
//		{
//			if(!mapConfig)return;
//			SoundManager.getInstance().stopAll(2);
//			switch(mapConfig.mapType)
//			{
//				case MapConfigVO.TWON_TYPE:
//				{
//					SoundManager.getInstance().play("Sound_Town",-1,1,5);
//					break;
//				}
//					
//				case MapConfigVO.FIGHTING_TYPE:
//				case MapConfigVO.FIGHT_TYPE:
//				{
//					SoundManager.getInstance().play("LieYanDong",-1,1,5);
//				}
//					
//				default:
//				{
//					
//					break;
//				}
//			}
//		}
		
		/**
		 * 播放
		 *  
		 * @param name	名称
		 * @param loop	循环次数，-1为无限循环
		 * @param volume	声音  -1为
		 * @param len	渐显需要的时间
		 */		
		public function play_core(name:String, loop:int=1, volume:Number=-1,len:Number=0):void
		{ 
			
			if(!name || name.length < 1)
			{
				return;
			}
			try
			{
				var ref:Class = getDefinitionByName(resUrl+name) as Class;
			}
			catch(e:Error){};
			
			var sound:Sound;
			if (ref)
			{
				sound = (new ref()) as Sound;
			}
			else
			{
				sound  = new Sound();
				sound.load(new URLRequest("music/"+name+".mp3"),new SoundLoaderContext(1000,true));
				sound.addEventListener(IOErrorEvent.IO_ERROR,soundCompleteListener);
			}
			
			var channel:SoundChannel = sound.play(0, loop != -1 ? loop : int.MAX_VALUE);
			if (!channel)
				return;
			
			if (loop != 0 && loop != -1)
				channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteListener);
			
			activeSound[name] = channel;
			
			if (len == 0)
			{
				channel.soundTransform = new SoundTransform((volume != -1) ? volume : defaultVolume);
			}
			else
			{
				channel.soundTransform = new SoundTransform(0);
				TweenMax.to(channel,len,{volume:(volume != -1) ? volume : defaultVolume});
			}
			
			function soundCompleteListener(evt:Event):void
			{
				if(channel)
					channel.removeEventListener(Event.SOUND_COMPLETE, soundCompleteListener);
				sound.removeEventListener(IOErrorEvent.IO_ERROR, soundCompleteListener);
				
				delete activeSound[name];
			}
		}
	}
}
